package com.ymatou.iapi.coupon.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.coupon.parameter.BatchInfo;
import com.ymatou.iapi.coupon.parameter.BatchInfos;
import com.ymatou.iapi.coupon.parameter.GetBatchInfoBean;
import com.ymatou.iapi.coupon.service.GetBatchInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;

/**
 * 获取批次信息请求信息用例
 * FreeMaker Template自动生成代码
 * 
 * @author chenjiazhu
 * 
 *         实现逻辑：
 *         select * from Ymt_CouponBatch
 *         inner join Ymt_CouponSetting
 *         on Ymt_CouponBatch.iCouponSettingId=Ymt_CouponSetting.iCouponSettingId
 *         where Ymt_CouponBatch.sBatchCode=@batchCode
 */
@TestSuite
public class Ts_GetBatchInfo {
    private static GetBatchInfoBean getbatchinfoBean;
    private static GetBatchInfoCall getbatchinfoCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("获取批次信息请求信息用例");
    }

    @Before
    public void caseUp() {
        getbatchinfoBean = new GetBatchInfoBean();
        getbatchinfoCall = new GetBatchInfoCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(getbatchinfoCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfo_001() {
        Logger.start(true, "获取批次信息请求信息用例-单条");
        try {
            List<Map> list = new YmtCouponBatchWapper().selectTop5BatchInfoBysBatchCode();

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(list.get(0).get("sBatchCode").toString());

            getbatchinfoBean.setBatchCodes(batchCodes);

            getbatchinfoCall.setData(getbatchinfoBean);
            getbatchinfoCall.callService();

            Logger.verifyEquals("0", getbatchinfoCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    getbatchinfoCall.getMessage(), "验证返回Message");

            verifyBatchInfos();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfo_002() {
        Logger.start(true, "获取批次信息请求信息用例-多条");
        try {
            List<Map> list = new YmtCouponBatchWapper().selectTop5BatchInfoBysBatchCode();

            List<String> batchCodes = new ArrayList<String>();

            for (Map batchCode : list) {
                batchCodes.add(batchCode.get("sBatchCode").toString());
            }

            getbatchinfoBean.setBatchCodes(batchCodes);

            getbatchinfoCall.setData(getbatchinfoBean);
            getbatchinfoCall.callService();

            Logger.verifyEquals("0", getbatchinfoCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    getbatchinfoCall.getMessage(), "验证返回Message");

            verifyBatchInfos();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfo_003() {
        Logger.start(true, "获取批次信息请求信息用例-单条商家优惠券批次-正常状态");
        try {
            CouponCallService couponCallService = new CouponCallService();
            String batchCode = couponCallService.createSellerDefaultCoupon();

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(batchCode);

            getbatchinfoBean.setBatchCodes(batchCodes);

            getbatchinfoCall.setData(getbatchinfoBean);
            getbatchinfoCall.callService();

            Logger.verifyEquals("0", getbatchinfoCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    getbatchinfoCall.getMessage(), "验证返回Message");

            BatchInfos batchInfos = getbatchinfoCall.getData();
            BatchInfo batchInfo = batchInfos.getBatchInfos().get(0);

            List<YmtCouponBatch> results = new YmtCouponBatchWapper().selectBysBatchCode(batchCode);

            Logger.verifyEquals(results.get(0).getiBatchId(), batchInfo.getBatchId(),
                    "验证返回BatchId与数据库表Ymt_CouponBatch中一致");
            Logger.verifyEquals(batchCode, batchInfo.getBatchCode(), "验证返回BatchCode");

            Logger.verifyEquals(0, batchInfo.getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfo_004() {
        Logger.start(true, "获取批次信息请求信息用例-单条-作废");
        try {
            CouponCallService couponCallService = new CouponCallService();
            String batchCode = couponCallService.createSellerDefaultCoupon();

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(batchCode);

            // 作废优惠券
            couponCallService.inValidCoupon(batchCode);

            getbatchinfoBean.setBatchCodes(batchCodes);

            getbatchinfoCall.setData(getbatchinfoBean);
            getbatchinfoCall.callService();

            Logger.verifyEquals("0", getbatchinfoCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    getbatchinfoCall.getMessage(), "验证返回Message");

            BatchInfos batchInfos = getbatchinfoCall.getData();
            BatchInfo batchInfo = batchInfos.getBatchInfos().get(0);

            List<YmtCouponBatch> results = new YmtCouponBatchWapper().selectBysBatchCode(batchCode);

            Logger.verifyEquals(results.get(0).getiBatchId(), batchInfo.getBatchId(),
                    "验证返回BatchId与数据库表Ymt_CouponBatch中一致");
            Logger.verifyEquals(batchCode, batchInfo.getBatchCode(), "验证返回BatchCode");
            // 验证状态为作废：1
            Logger.verifyEquals(1, batchInfo.getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfo_005() {
        Logger.start(true, "获取批次信息请求信息用例-单条-过期");
        try {
            CouponCallService couponCallService = new CouponCallService();
            String batchCode = couponCallService.createSellerDefaultCoupon();

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(batchCode);

            // 设置优惠券过期
            Date date = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
            int iCouponSettingId =
                    new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0).getiCouponSettingId();
            new YmtCouponSettingWapper().setiEffectiveType(iCouponSettingId, 0);
            new YmtCouponSettingWapper().setdValidEnd(iCouponSettingId, date);

            getbatchinfoBean.setBatchCodes(batchCodes);

            getbatchinfoCall.setData(getbatchinfoBean);
            getbatchinfoCall.callService();

            Logger.verifyEquals("0", getbatchinfoCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    getbatchinfoCall.getMessage(), "验证返回Message");

            BatchInfos batchInfos = getbatchinfoCall.getData();
            BatchInfo batchInfo = batchInfos.getBatchInfos().get(0);

            List<YmtCouponBatch> results = new YmtCouponBatchWapper().selectBysBatchCode(batchCode);

            Logger.verifyEquals(results.get(0).getiBatchId(), batchInfo.getBatchId(),
                    "验证返回BatchId与数据库表Ymt_CouponBatch中一致");
            Logger.verifyEquals(batchCode, batchInfo.getBatchCode(), "验证返回BatchCode");
            // 验证状态为过期：2
            Logger.verifyEquals(2, batchInfo.getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfo_006() {
        Logger.start(true, "获取批次信息请求信息用例-单条-领完");
        try {
            CouponCallService couponCallService = new CouponCallService();
            String batchCode = couponCallService.createSellerDefaultCoupon();

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(batchCode);

            // 设置优惠券领完
            YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
            int iCouponSettingId = batch.getiCouponSettingId();
            new YmtCouponSettingWapper().setiReceiveCount(iCouponSettingId, batch.getiCouponTotalNum());

            getbatchinfoBean.setBatchCodes(batchCodes);

            getbatchinfoCall.setData(getbatchinfoBean);
            getbatchinfoCall.callService();

            Logger.verifyEquals("0", getbatchinfoCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    getbatchinfoCall.getMessage(), "验证返回Message");

            BatchInfos batchInfos = getbatchinfoCall.getData();
            BatchInfo batchInfo = batchInfos.getBatchInfos().get(0);

            List<YmtCouponBatch> results = new YmtCouponBatchWapper().selectBysBatchCode(batchCode);

            Logger.verifyEquals(results.get(0).getiBatchId(), batchInfo.getBatchId(),
                    "验证返回BatchId与数据库表Ymt_CouponBatch中一致");
            Logger.verifyEquals(batchCode, batchInfo.getBatchCode(), "验证返回BatchCode");
            // 验证状态为过期：3
            Logger.verifyEquals(3, batchInfo.getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfo_007() {
        Logger.start(true, "获取批次信息请求信息用例-多条多种状态组合");
        try {
            CouponCallService couponCallService = new CouponCallService();
            String batchCode1 = couponCallService.createSellerDefaultCoupon();
            String batchCode2 = couponCallService.createSellerDefaultCoupon();
            String batchCode3 = couponCallService.createSellerDefaultCoupon();
            String batchCode4 = couponCallService.createSellerDefaultCoupon();
            String batchCode5 = couponCallService.createSellerDefaultCoupon();

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(batchCode1);
            batchCodes.add(batchCode2);
            batchCodes.add(batchCode3);
            batchCodes.add(batchCode4);
            batchCodes.add(batchCode5);

            // 作废优惠券
            couponCallService.inValidCoupon(batchCode2);

            // 设置优惠券过期
            Date date = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
            int iCouponSettingId3 =
                    new YmtCouponBatchWapper().selectBysBatchCode(batchCode3).get(0).getiCouponSettingId();
            new YmtCouponSettingWapper().setiEffectiveType(iCouponSettingId3, 0);
            new YmtCouponSettingWapper().setdValidEnd(iCouponSettingId3, date);

            // 设置优惠券领完
            YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode5).get(0);
            int iCouponSettingId4 = batch.getiCouponSettingId();
            new YmtCouponSettingWapper().setiReceiveCount(iCouponSettingId4, batch.getiCouponTotalNum());

            getbatchinfoBean.setBatchCodes(batchCodes);

            getbatchinfoCall.setData(getbatchinfoBean);
            getbatchinfoCall.callService();

            Logger.verifyEquals("0", getbatchinfoCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    getbatchinfoCall.getMessage(), "验证返回Message");

            BatchInfos batchInfos = getbatchinfoCall.getData();

            for (BatchInfo batchInfo : batchInfos.getBatchInfos()) {
                List<YmtCouponBatch> results = new YmtCouponBatchWapper().selectBysBatchCode(batchInfo.getBatchCode());

                Logger.verifyEquals(results.get(0).getiBatchId(), batchInfo.getBatchId(),
                        "验证返回BatchId与数据库表Ymt_CouponBatch中一致");
            }

            // 验证状态
            Logger.verifyEquals(0, batchInfos.getBatchInfos().get(0).getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");
            Logger.verifyEquals(1, batchInfos.getBatchInfos().get(1).getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");
            Logger.verifyEquals(2, batchInfos.getBatchInfos().get(2).getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");
            Logger.verifyEquals(0, batchInfos.getBatchInfos().get(3).getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");
            Logger.verifyEquals(3, batchInfos.getBatchInfos().get(4).getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    private void verifyBatchInfos()
    {
        List<BatchInfo> batchInfos = getbatchinfoCall.getData().getBatchInfos();

        List<String> batchCodes = getbatchinfoBean.getBatchCodes();
        int num=batchCodes.size();

        for (int i = 0; i < num; i++) {
            BatchInfo batchInfo = batchInfos.get(num-1-i);
            YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchInfo.getBatchCode()).get(0);

            Logger.verifyEquals(batchCodes.get(i), batchInfo.getBatchCode(), "验证返回BatchStatusInfo为正常");
            Logger.verifyEquals(batch.getiBatchId(), batchInfo.getBatchId(), "验证返回BatchStatusInfo为正常");
        }
    }

    private void verifyStatus(int BatchStatusInfo)
    {
        List<BatchInfo> batchInfos = getbatchinfoCall.getData().getBatchInfos();

        for (int i = 0; i < batchInfos.size(); i++) {
            BatchInfo batchInfo = batchInfos.get(i);
            Logger.verifyEquals(BatchStatusInfo, batchInfo.getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");
        }
    }
}
